package joke.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import joke.deal.ReRiddles;
import joke.ibatis.po.ParameterPo;
import joke.ibatis.po.RiddlesPo;
import net.sf.json.JSONObject;

public class RiddlesByOrderServlet extends HttpServlet {
	private static final String CONTENT_TYPE = "application/json;charset=UTF-8";

	private static final long serialVersionUID = 1L;

	public void init(ServletConfig config) throws ServletException {
		super.init(config);
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType(CONTENT_TYPE);
		response.setCharacterEncoding("UTF-8");
		PrintWriter pw=null;
		try {
			String id = request.getParameter("id");
			String type = request.getParameter("type");
			ParameterPo parpo = new ParameterPo();
			parpo.setId(id);
			parpo.setType(type);
			ReRiddles cb = new ReRiddles();
			RiddlesPo po = cb.viewRiddlesByOrder(parpo);
			JSONObject jsonObject = JSONObject.fromObject(po);
			pw = response.getWriter();
			pw.write(jsonObject.toString());
			pw.flush();
		} catch (IOException ioe) {
			ioe.printStackTrace();
		}finally {
			try {
				if (pw != null)
					pw.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}
}
